package com.ds.base.utils;

import java.security.MessageDigest;

/**
 *
 * @author zhangyou
 * @date 2018/9/15
 */
public class Md5Encoder {







    public static String generateCode(String source) {
        if(source != null && !"".equals(source)) {
            byte[] sourceByte = source.getBytes();
            return digest(sourceByte);
        } else {
            return "";
        }
    }




    public static String generateCode(byte[] sourceByte) {
        if(sourceByte != null && !"".equals(sourceByte)) {
           return digest(sourceByte);
        } else {
            return "";
        }
    }



    private static String digest(byte[] sourceByte){
        String target = null;
        try {
            char[] hexDigits = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(sourceByte);
            byte[] tmp = md.digest();
            char[] str = new char[32];
            int k = 0;

            for(int i = 0; i < 16; ++i) {
                byte byte0 = tmp[i];
                str[k++] = hexDigits[byte0 >>> 4 & 15];
                str[k++] = hexDigits[byte0 & 15];
            }

            target = new String(str);
            return target;
        } catch (Exception var10) {
            var10.printStackTrace();
            return "";
        }
    }











}
